package com.wanmait.wanmarecruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.wanmarecruit.annotation.ManageLogAnnotation;
import com.wanmait.wanmarecruit.entity.City;
import com.wanmait.wanmarecruit.entity.CompanySize;
import com.wanmait.wanmarecruit.service.CompanyService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wanmarecruit.entity.Company;
import com.wanmait.wanmarecruit.mapper.CompanyMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;

    @Override
    public List<Company> getRandomEightHotCompany() {
        return companyMapper.selectRandomEightHotCompany();
    }

    @Override
    public Company selectCompanyByCompanyId(Integer companyId) {
        return companyMapper.selectCompanyByCompanyId(companyId);
    }

    @Override
    public PageInfo<Company> selectCompaniesByCondition(Company company, City city, Integer search, Integer[] companySizeIds, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(companyMapper.selectCompaniesByCondition(company,city,search,companySizeIds),5);
    }

    @Override
    public Integer getCountByCompanySize(Integer companySizeId) {
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getCompanySizeId,companySizeId);
        return (int)this.count(queryWrapper);
    }

    @Override
    public Company checkPhoneAndPass(Company company) {
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getPhone,company.getPhone());
        queryWrapper.eq(Company::getPass,company.getPass());
        return this.getOne(queryWrapper);
    }

    @Override
    public PageInfo<Company> selectAllCompany(Integer pageNum, Integer pageSize,String positionTypeId,String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(companyMapper.selectAllCompany(positionTypeId, keyword),5);
    }

    @Override
    public PageInfo<Company> selectAuditCompanies(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(companyMapper.selectAuditCompanies());
    }

    @Override
    @ManageLogAnnotation(module = "公司审核",info = "审核通过了一家公司")
    public void updateEnableCompany(Integer id) {
        companyMapper.updateEnableCompany(id);
    }

    @Override
    @ManageLogAnnotation(module = "公司审核",info = "拒绝了一家公司的注册")
    public void deleteOneCompany(Integer id) {
        companyMapper.deleteOneCompany(id);
    }

    @Override
    public Company getOneCompanyByNameAndLegal(String name, String legal) {
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getName,name);
        queryWrapper.eq(Company::getLegalRepresentative,legal);
        return this.getOne(queryWrapper);
    }

    @Override
    @ManageLogAnnotation(module = "公司管理",info = "已将该公司封禁")
    public void updateCompanyEnableFalseByCompanyId(Integer companyId) {
        UpdateWrapper<Company> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("enable",0).in("id",companyId);
        this.update(updateWrapper);
    }

    @Override
    public Long getCompanyCount() {
        return this.count();
    }
}
